package treeDemo;

import org.junit.Assert;
import org.junit.Test;

import java.util.List;

import static treeDemo.TestIrregularTree.readFileToTree;

/**
 * 测试满树的情况  树的节点情况是 : 1-234,2-567,3-8,4-9、10
 * @author 1
 *
 */
public class TestFullTree {

    private final Tree tree = readFileToTree("D:/eclipse/fullTree.json");

    @Test
    public void testDfs() {
        // 测试深度优先遍历是否正确
        // 非递归方法
        List<TreeNode> listdfs = tree.depthFirstSearch();
        StringBuilder dfs = new StringBuilder();
        for (TreeNode listdf : listdfs) {
            dfs.append(listdf.getId()).append(" ");
        }
        String dfsActual = dfs.toString();
        String dfsExpected = "1 2 5 6 7 3 8 4 9 10 ";
        Assert.assertEquals(dfsExpected, dfsActual);

    }

    @Test
    public void testRecursiveDfs() {
        // 递归的方法
        List<TreeNode> recursivedfs = tree.recursiveDFS();
        StringBuilder recdfs = new StringBuilder();
        for (TreeNode recursivedf : recursivedfs) {
            recdfs.append(recursivedf.getId()).append(" ");
        }
        String recdfsActual = recdfs.toString();
        String recdfsExpected = "1 2 5 6 7 3 8 4 9 10 ";
        Assert.assertEquals(recdfsActual, recdfsExpected);
    }

    @Test
    public void testBfs() {
        // 测试广度优先遍历是否正确
        List<TreeNode> listbfs = tree.breathFirstSearch();
        StringBuilder bfs = new StringBuilder();
        for (TreeNode listbf : listbfs) {
            bfs.append(listbf.getId()).append(" ");
        }
        String bfsActual = bfs.toString();
        String bfsExpected = "1 2 3 4 5 6 7 8 9 10 ";
        Assert.assertEquals(bfsExpected, bfsActual);
    }

    @Test
    public void testGetLevelNodes() {
        // 测试get某一层节点列表是否正确
        // 循环测试每一层
        String[] expectedList = { "1 ", "2 3 4 ", "5 6 7 8 9 10 " };
        for (int i = 1; i <= tree.getMaxDepth(tree.getRoot()); i++) {
            List<TreeNode> eachLevel = tree.getLevelNodeList(i);
            StringBuilder sb = new StringBuilder();
            for (TreeNode n : eachLevel) {
                sb.append(n.getId()).append(" ");
            }
            String actual = sb.toString();
            Assert.assertEquals(expectedList[i - 1], actual);
        }
    }

    @Test
    public void testGetMaxDepth() {
        // 测试最大深度是否正确
        Assert.assertEquals(3, tree.getMaxDepth(tree.getRoot()));
    }

    @Test
    public void testMaxDistance() {
        // 测试根节点到叶子节点的最大、最小距离是否正确
        Assert.assertEquals(2, tree.maxDistance());
    }

    @Test
    public void testMinDistance() {
        Assert.assertEquals(2, tree.minDistance());
    }

    @Test
    public void testFindNode() {
        String nameExpected = "ArrayList";
        Assert.assertEquals(nameExpected, tree.findNode(5).getName());
    }
}
